﻿package plugin.roleN.controller
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.display.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.geom.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.instance.*;
    import pluginfw.nio.*;
    import uiresource.roleui.*;
    import view.*;
    import view.mornExtends.*;
    import vo.*;

    public class ZhuanShengCtrl extends ZhuanShengUI
    {
        private var attarr:Array;
        private var isinit:Boolean = false;
        private var cfgrein:Object;
        private var nengliangMc:XMLMovieClip;
        private var faceMc:XMLMovieClip;
        private var energy:int;
        private var mapID:int;
        private var career:int;
        private var reinlv:int;
        private var userdata:Object;
        private var cfglevel:Object;
        private var cfgitem:Object;
        private var cfgstore:Object;
        private var storeID:Array;
        private var successMc:XMLMovieClip;
        private var seriverid:int;
        private var globalid:Object;
        private var xiuweiexp:int;
        public static const zsexpboll:Class = ZhuanShengCtrl_zsexpboll;
        public static const expface:Class = ZhuanShengCtrl_expface;
        public static const successEffect:Class = ZhuanShengCtrl_successEffect;

        public function ZhuanShengCtrl()
        {
            this.attarr = ["攻击: ", "防御: ", "生命: ", "精准: ", "闪避: "];
            this.storeID = [202, 203];
            return;
        }// end function

        override protected function initializeView() : void
        {
            var _loc_3:Array = null;
            super.initializeView();
            zhuanshengbg1.url = null;
            zhuanshengbg2.url = null;
            zhuanshengbg3.url = null;
            App.mloader3.loadSWF(Config.getUrl("assets/images/mainui/zhuansheng.swf"), 1, new Handler(this.completeFunc1));
            txt_fb.label = "获得修为";
            this.isinit = true;
            rigth_box.visible = false;
            this.globalid = ConfigDictionary.data.global;
            var _loc_1:* = (this.globalid[1].value as String).split(";");
            this.seriverid = Config.gameParams.serverid;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.length)
            {
                
                if (_loc_1[_loc_2])
                {
                    _loc_3 = (_loc_1[_loc_2] as String).split("#");
                    if (this.seriverid >= _loc_3[0] && this.seriverid <= _loc_3[1])
                    {
                        this.xiuweiexp = _loc_3[2];
                        break;
                    }
                }
                _loc_2++;
            }
            this.cfgitem = ConfigDictionary.data.item;
            this.cfgstore = ConfigDictionary.data.storeNew;
            this.showRigth();
            this.listener();
            return;
        }// end function

        private function completeFunc1(param1:Object) : void
        {
            var _loc_2:* = new XML(new successEffect());
            this.successMc = new XMLMovieClip("png.zhuansheng", _loc_2);
            this.successMc.mouseChildren = false;
            this.successMc.mouseEnabled = false;
            this.successMc.x = 15;
            this.successMc.y = -50;
            this.successMc.visible = false;
            this.successMc.play();
            this.addChild(this.successMc);
            return;
        }// end function

        protected function duihuan(event:MouseEvent) : void
        {
            PluginManager.instance.sendToBufferServer("zhuansheng", CMDRein.GetXiuWei, new Output());
            UIGlobel.pointsDic["zhuanshengeffpos"] = this.localToGlobal(new Point(this.zsLv1.x, this.zsLv1.y));
            ModelProxy.change("mainui", "flyeff", 6);
            return;
        }// end function

        protected function openRigthBox(event:MouseEvent) : void
        {
            rigth_box.visible = !rigth_box.visible;
            return;
        }// end function

        protected function closeRigthBox(event:MouseEvent) : void
        {
            rigth_box.visible = false;
            return;
        }// end function

        private function loadHpClip() : void
        {
            return;
        }// end function

        private function listener() : void
        {
            btn_zs.addEventListener(MouseEvent.CLICK, this.onZhuanSheng);
            closebtn.addEventListener(MouseEvent.CLICK, this.closeRigthBox);
            txt_fb.addEventListener(MouseEvent.CLICK, this.openRigthBox);
            btn_duihuan.addEventListener(MouseEvent.CLICK, this.duihuan);
            return;
        }// end function

        protected function onRollOver(event:MouseEvent) : void
        {
            var _loc_2:String = "达到等级上限后，累加经验可增加转生进度，成功转生后，会返还50%的经验";
            ModelProxy.change("tips", "normal", {data:_loc_2, p:new Point()});
            return;
        }// end function

        protected function onRollOut(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        protected function onZhuanSheng(event:MouseEvent) : void
        {
            var _loc_2:* = int(UserData.getUserData("rein"));
            if (this.cfgrein == null)
            {
                this.cfgrein = ConfigDictionary.data.rein;
            }
            var _loc_3:* = this.cfgrein[_loc_2].needEnergy;
            if (this.energy >= _loc_3)
            {
                PluginManager.instance.sendToBufferServer("zhuansheng", CMDRein.DoRein, new Output());
            }
            else
            {
                WarningManager.show(56);
            }
            return;
        }// end function

        public function init() : void
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            if (this.isinit == false)
            {
                return;
            }
            this.userdata = ModelProxy.getValue("data", "user");
            var _loc_1:* = UserData.getOnlineData();
            if (this.cfglevel == null)
            {
                this.cfglevel = ConfigDictionary.data.level;
            }
            this.reinlv = this.userdata.rein;
            if (this.reinlv >= 15)
            {
                btn_zs.visible = false;
            }
            else
            {
                btn_zs.visible = true;
            }
            this.career = this.userdata.career;
            if (this.cfgrein == null)
            {
                this.cfgrein = ConfigDictionary.data.rein;
            }
            if (this.cfgrein[this.reinlv])
            {
                _loc_3 = this.cfgrein[this.reinlv];
                zsLv.text = _loc_3.needEnergy;
                this.getAttribute();
                this.showLevel(this.userdata.level, lv, lv1, lv3);
                this.showLevel((this.userdata.level - 1), lv4, lv5, lv6);
                if (this.userdata.level > this.cfgrein[0].needLevel)
                {
                    _loc_4 = int(_loc_3.exchangenum) - _loc_1.reinnum;
                    if (_loc_4 > 0)
                    {
                        txt_times.text = "今日剩余兑换次数：<font color=\'#31cf08\'>" + _loc_4 + "</font>";
                        btn_duihuan.disabled = false;
                    }
                    else
                    {
                        btn_duihuan.disabled = true;
                        txt_times.text = "今日剩余兑换次数：<font color=\'#ff0000\'>" + _loc_4 + "</font>";
                    }
                    if (this.cfglevel[(this.userdata.level - 1)])
                    {
                        txt_exp.text = int(int(this.cfglevel[(this.userdata.level - 1)]) / this.xiuweiexp) + "";
                    }
                }
                else
                {
                    btn_duihuan.disabled = true;
                    txt_times.text = "<font color=\'#ff0000\'>" + (int(this.cfgrein[0].needLevel) + 1) + "</font><font color=\'#94928c\'> 级以上才可以使用</font>";
                    txt_exp.text = 0 + "";
                }
            }
            var _loc_2:* = ModelProxy.getValue("zhuan", "getatt");
            this.reinEnergy(_loc_2);
            return;
        }// end function

        public function zhuansucc() : void
        {
            if (this.successMc)
            {
                this.successMc.visible = true;
                this.successMc.playFromTo(-1, (this.successMc.totalFrame - 1), new Handler(this.doEnd1));
            }
            return;
        }// end function

        private function doEnd1() : void
        {
            this.successMc.visible = false;
            this.successMc.stop();
            return;
        }// end function

        private function showLevel(param1:int, param2:Image, param3:Image, param4:Image) : void
        {
            if (param1 < 100)
            {
                param4.visible = false;
                param2.url = "png.common." + int(param1 / 10);
                param3.url = "png.common." + param1 % 10;
            }
            else
            {
                param4.visible = true;
                param2.url = "png.common." + String(param1).charAt(0);
                param3.url = "png.common." + String(param1).charAt(1);
                param4.url = "png.common." + String(param1).charAt(2);
            }
            return;
        }// end function

        private function showRigth() : void
        {
            list.renderHandler = new Handler(this.renderHandler);
            list.array = this.storeID;
            return;
        }// end function

        private function renderHandler(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            var _loc_5:FlashItem = null;
            var _loc_6:Label = null;
            var _loc_7:Label = null;
            var _loc_8:Button = null;
            if (param2 < this.storeID.length)
            {
                _loc_3 = this.cfgstore[this.storeID[param2]];
                _loc_4 = ItemObject.create(this.cfgitem[_loc_3.itemid]);
                _loc_5 = param1.getChildByName("fitem") as FlashItem;
                _loc_5.data = _loc_4;
                _loc_6 = param1.getChildByName("txt_name") as Label;
                _loc_6.text = _loc_4.name;
                _loc_7 = param1.getChildByName("labelmoney") as Label;
                _loc_7.text = _loc_3.nowprice;
                _loc_8 = param1.getChildByName("btn_shiyong") as Button;
                _loc_8.clickHandler = new Handler(this.gobuy, [_loc_3]);
            }
            return;
        }// end function

        private function gobuy(param1:Object) : void
        {
            var _loc_2:* = UserData.getBagData("money");
            if (_loc_2 < param1.nowprice)
            {
                ModelProxy.change("movetips", "error", "元宝不足，无法购买该道具！");
                return;
            }
            DialogMgr.instance.open(AppDialog.SC_BUY, {storeId:param1.id});
            return;
        }// end function

        private function onComplete() : void
        {
            this.faceMc.visible = false;
            return;
        }// end function

        public function reinEnergy(param1:Object) : void
        {
            var _loc_2:* = int(UserData.getUserData("rein"));
            if (this.cfgrein == null)
            {
                this.cfgrein = ConfigDictionary.data.rein;
            }
            var _loc_3:* = this.cfgrein[_loc_2].needEnergy;
            zsLv.text = _loc_3 + "";
            if (param1)
            {
                if (param1.reinEnergy >= _loc_3)
                {
                    zsLv1.color = 16770247;
                }
                else
                {
                    zsLv1.color = 16711680;
                }
                zsLv1.text = "" + int(param1.reinEnergy);
                this.energy = int(param1.reinEnergy);
            }
            else
            {
                zsLv1.color = 16711680;
                zsLv1.text = "0";
            }
            return;
        }// end function

        public function getAttribute() : void
        {
            var _loc_5:Array = null;
            var _loc_6:Array = null;
            var _loc_7:Object = null;
            var _loc_8:int = 0;
            var _loc_9:Object = null;
            var _loc_1:* = ModelProxy.getValue("data", "user.rein");
            var _loc_2:* = this.cfgrein[this.reinlv];
            var _loc_3:* = this.cfgrein[(this.reinlv + 1)];
            if (_loc_1 < 10)
            {
                rein_img1.url = "cs2_zhuansheng/" + _loc_1 + ".png";
                rein_img2.visible = false;
                zhuan_img.x = 303;
            }
            else
            {
                zhuan_img.x = 323;
                rein_img2.visible = true;
                rein_img1.url = "cs2_zhuansheng/" + _loc_1.toString().charAt(0) + ".png";
                rein_img2.url = "cs2_zhuansheng/" + _loc_1.toString().charAt(1) + ".png";
            }
            if (_loc_2)
            {
                _loc_5 = [];
                _loc_5.push(_loc_2.att + " - " + _loc_2.attMax);
                _loc_5.push(_loc_2.def + " - " + _loc_2.defMax);
                _loc_5.push(_loc_2.hp + "%");
                _loc_5.push(_loc_2.accurate);
                _loc_5.push(_loc_2.dodge);
                list1.array = _loc_5;
            }
            if (_loc_3)
            {
                _loc_6 = [];
                _loc_6.push(_loc_3.att + " - " + _loc_3.attMax);
                _loc_6.push(_loc_3.def + " - " + _loc_3.defMax);
                _loc_6.push(_loc_3.hp + "%");
                _loc_6.push(_loc_3.accurate);
                _loc_6.push(_loc_3.dodge);
                list2.array = _loc_6;
            }
            if (this.reinlv < 15)
            {
                list2.visible = true;
            }
            else
            {
                list2.visible = false;
            }
            list1.renderHandler = new Handler(this.list1Handler);
            list2.renderHandler = new Handler(this.list2Handler);
            var _loc_4:* = ModelProxy.getValue("data", "jingjitypedata");
            if (ModelProxy.getValue("data", "jingjitypedata") == null)
            {
                return;
            }
            if (_loc_4[3])
            {
                _loc_7 = ConfigDictionary.data.activitygoal;
                for each (_loc_9 in _loc_7[3])
                {
                    
                    _loc_8 = _loc_8 + 1;
                    if (_loc_4[3][0][(_loc_8 - 1)].isGet == 0 && _loc_1 >= _loc_9.goal)
                    {
                        if (_loc_4[3][0][(_loc_8 - 1)].count < _loc_9.count)
                        {
                            ModelProxy.change("mainui", "showEffect", {ab:"kaifujingji", state:true});
                            break;
                        }
                    }
                }
            }
            return;
        }// end function

        private function list1Handler(param1:Component, param2:int) : void
        {
            var _loc_3:Label = null;
            if (param2 < list1.array.length)
            {
                _loc_3 = param1.getChildByName("txt1") as Label;
                _loc_3.text = this.attarr[param2] + list1.array[param2];
            }
            return;
        }// end function

        private function list2Handler(param1:Component, param2:int) : void
        {
            var _loc_3:Label = null;
            if (param2 < list2.array.length)
            {
                _loc_3 = param1.getChildByName("txt2") as Label;
                _loc_3.text = this.attarr[param2] + list2.array[param2];
            }
            return;
        }// end function

        private function getCareer(param1:int) : String
        {
            var _loc_2:String = "";
            switch(param1)
            {
                case 1:
                {
                    _loc_2 = "物攻";
                    break;
                }
                case 2:
                {
                    _loc_2 = "法功";
                    break;
                }
                case 3:
                {
                    _loc_2 = "道攻";
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_2;
        }// end function

        private function onZsfb(event:MouseEvent) : void
        {
            PluginManager.instance.sendToServer("instance", CMDInstance.NpcInstance, {mapid:this.mapID});
            return;
        }// end function

    }
}
